package com.app.controller;

import com.app.common.ConstantTool;
import com.app.common.Result;
import com.app.common.SystemConstant;
import com.app.model.AdminUserModel;
import com.app.model.FileModel;
import com.app.model.UserModel;
import com.app.server.AdminUserService;
import com.app.server.FileService;
import com.app.utils.TokenUtils;
import com.app.vo.AdminUserVo;
import com.app.vo.ConstantVo;
import com.app.vo.DisasterReportingVo;
import com.app.vo.FileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * 文件操作
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/file")
public class FileController {
    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private FileService fileService;

    // 管理后台查询文件
    @GetMapping("/getFileList")
    public Result getFileList(Integer page, Integer limit, Integer type, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }


        FileVo fileList = fileService.getFileList(page, limit, type);
        return Result.success(fileList);
    }

    // 管理后台查询文件详情
    @GetMapping("/getFileById")
    public Result getFileById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        FileModel fileModel = fileService.getById(id);
        return Result.success(fileModel);
    }

    // 管理后台查询文件详情
    @GetMapping("/getConstant")
    public Result getConstant(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        List<ConstantVo> fileTypeList = ConstantTool.CONSTANT_VO_LIST;
        return Result.success(fileTypeList);
    }

    // 管理后台删除文件
    @GetMapping("/delFile")
    public Result delFile(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        FileModel fileModel = fileService.getById(id);
        try {
            File file1 = new File(SystemConstant.DEFAULT_UPLOAD_URL + fileModel.getName());
            if (file1.delete()) {
                System.out.println(file1.getName() + " 图片已被删除！");
            } else {
                System.out.println("删除图片操作失败！");
            }
        } catch (Exception e) {
            return Result.success(e);
        }
        fileService.removeById(id);
        return Result.success("删除成功");
    }


    // 管理后台删除文件
    @GetMapping("/recallFile")
    public Result recallFile(String filePath, HttpServletRequest request) {
        try {
            File file1 = new File(filePath);
            if (file1.delete()) {
                System.out.println(file1.getName() + " 图片已被删除！");
            } else {
                System.out.println("删除图片操作失败！");
            }
        } catch (Exception e) {
            return Result.success(e);
        }
        return Result.success("删除成功");
    }
}
